home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 109_01.zip / UTIL-WP.C < prev    next >
Text File  |  1993-06-26  |  3KB  |  257 lines

  1. /*
  2.     Utility functions for word processor
  3. */
  4.  
  5. #include <macdefs.wp>
  6. #include <globals.wp>
  7.  
  8. #define fputc putc    /* I keep getting it wrong */
  9.  
  10. /*
  11.     copy title from buf to ttl
  12. */
  13.  
  14. gettl(buf,ttl)
  15.     char *buf;
  16.     char *ttl;
  17.     {
  18.  
  19.     while ( ! isspace(*buf) )
  20.       buf++;
  21.     while ( isspace(*buf) )
  22.       buf++;
  23.     if ( *buf == '\'' || *buf == '"' )
  24.       buf++;
  25.     strcpy(ttl,buf);
  26.     }
  27.  
  28.  
  29. /*
  30.     space n lines or to bottom of page
  31. */
  32.  
  33. space(n)
  34.     int n;
  35.     {
  36.  
  37.     brk();
  38.     if ( lineno > bottom )
  39.       return;
  40.     if( lineno == 0 )
  41.       phead();
  42.     skip(min(n,bottom+1-lineno));
  43.     lineno += n;
  44.     if ( lineno > bottom )
  45.       pfoot();
  46.     }
  47.  
  48.  
  49. /*
  50.     put out a line with proper spacing and indenting
  51. */
  52.  
  53. put(buf)
  54.     char buf[];
  55.     {
  56.     int i;
  57.  
  58.     if ( lineno == 0 || lineno > bottom )
  59.       phead();
  60.     for ( i=0; i < tival; i++ )
  61.       fputc(' ',outfile);
  62.     tival = inval;
  63.     fputs(buf,outfile);
  64.     skip(min(lsval-1,bottom-lineno));
  65.     lineno += lsval;
  66.     if ( lineno > bottom )
  67.       pfoot();
  68.     }
  69.  
  70.  
  71. /*
  72.     delete leading blanks and set tival
  73. */
  74.  
  75. leadbl(buf)
  76.     char buf[];
  77.     {
  78.     int i;
  79.  
  80.     brk();
  81.     for ( i = 0; buf[i] == ' '; i++ )
  82.       ;
  83.     if ( buf[i] != '\n' )
  84.       tival = i;
  85.     strcpy(buf,&buf[i]);
  86.     }
  87.  
  88.  
  89.  
  90.  
  91. /*
  92.     put out page header
  93. */
  94.  
  95. phead()
  96.     {
  97.  
  98.     curpag = newpag++;
  99.     if ( m1val > 0 ) {
  100.       skip(m1val-1);
  101.       puttl(header,curpag);
  102.       }
  103.     skip(m2val);
  104.     lineno = m1val + m2val + 1;
  105.     }
  106.  
  107.  
  108.  
  109. /*
  110.     put out page footer
  111. */
  112.  
  113. pfoot()
  114.     {
  115.  
  116.     skip(m3val);
  117.     if ( m4val > 0 )
  118.       puttl(footer,curpag);
  119.     skip(m4val-1);
  120.     lineno = 0;
  121.     }
  122.  
  123.  
  124.  
  125. /*
  126.     put out title line with optional page number
  127. */
  128.  
  129. puttl(buf,pageno)
  130.     char *buf;
  131.     int pageno;
  132.     {
  133.     char c;
  134.  
  135.     while ( c = *buf++ )
  136.       if ( c == '#' )
  137.         fprintf(outfile,"%4d",pageno);
  138.       else
  139.         fputc(c,outfile);
  140.     }
  141.  
  142. /*
  143.     get a non-blank word from in[i] to out[]
  144.     and advance i
  145.     return length of out[]
  146. */
  147.  
  148. getwrd(in,i,out)
  149.     char in[];
  150.     int *i;
  151.     char out[];
  152.     {
  153.     int ii;
  154.     int j;
  155.     char c;
  156.  
  157.     ii = *i;
  158.     while ( (c=in[ii]) == ' ' || c == '\t' )
  159.       ++ii;
  160.     j = 0;
  161.     while ( (c=in[ii]) != ' ' && c != '\t' && c != '\n' && c != '\0' )
  162.       out[j++] = in[ii++];
  163.     out[j] = '\0';
  164.     *i = ii;
  165.     return j;
  166.     }
  167.  
  168.  
  169. /*
  170.     output n blank lines
  171. */
  172.  
  173. skip(n)
  174.     int n;
  175.     {
  176.     int i;
  177.  
  178.     for ( i = 0; i < n; i++ ) {
  179.       putc('\r',outfile);
  180.       putc('\n',outfile);
  181.       }
  182.     }
  183.  
  184.  
  185.  
  186. /*
  187.     minimum of two arguments
  188. */
  189.  
  190. min(a,b)
  191.     int a;
  192.     int b;
  193.     {
  194.     return a < b ? a : b;
  195.     }
  196.  
  197.  
  198.  
  199. /*
  200.     maximum of two arguments
  201. */
  202.  
  203. max(a,b)
  204.     int a;
  205.     int b;
  206.     {
  207.     return a > b ? a : b;
  208.     }
  209.  
  210.  
  211. /*
  212.     compare strings for equality
  213.     make upper and lower case equivalent
  214. */
  215.  
  216. samestr(str1,str2)
  217.     char *str1,*str2;
  218.     {
  219.     while ( *str1 )
  220.       if ( toupper(*str1++) != toupper(*str2++) )
  221.         return 0;
  222.     if ( *str2 != '\0' )
  223.       return 0;
  224.     return 1;
  225.     }
  226.  
  227.  
  228. /*    -------------------------------------------------------
  229.  
  230.     Name:        index(s,t)
  231.     Result:        position of s in t
  232.     Errors:        notfound
  233.     Globals:    ---
  234.     Macros:        ---
  235.     Procedures:
  236.  
  237.     Action:        Return the position (index) in the
  238.             string s where string t begins,
  239.             or -1 if s doesn't contain t.
  240.             Uses 0 as starting position in s
  241.  
  242.     ------------------------------------------------------- */
  243.  
  244. index(s, t)
  245.     char s[], t[];
  246.     {
  247.     int i, j, k;
  248.  
  249.     for (i = 0; s[i] != '\0'; i++) {
  250.       for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
  251.         ;
  252.       if (t[k] == '\0')
  253.         return(i);
  254.       }
  255.     return -1;
  256.     }
  257.